package com.lzy.mall.product.controller;


import cn.hutool.core.collection.CollUtil;
import com.aliyun.oss.OSS;
import com.lzy.mall.common.utils.ResultUtils;
import com.lzy.mall.product.entity.PmsCategory;
import com.lzy.mall.product.service.PmsCategoryService;
import com.lzy.mall.product.vo.PmsCategoryVo;
import org.springframework.web.bind.annotation.*;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 商品三级分类(PmsCategory)表控制层
 *
 * @author lzy
 * @since 2021-08-25 11:41:12
 */
@RestController
@RequestMapping("category")
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class PmsCategoryController {

    private final PmsCategoryService pmsCategoryService;

    private final OSS ossClient;

    @GetMapping("/upload")
    public void upload(){
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream("F:\\Java2EE\\yupian\\20190323161418695.png");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        // 依次填写Bucket名称（例如examplebucket）和Object完整路径（例如exampledir/exampleobject.txt）。Object完整路径中不能包含Bucket名称。
        ossClient.putObject("lzy-mall", "65.png", inputStream);

        // 关闭OSSClient。
        ossClient.shutdown();

        System.out.println("上传成功");
    }

    @GetMapping("/list/tree")
    public ResultUtils getTree3(){
        List<PmsCategoryVo> pmsCategoryVos = pmsCategoryService.getTree3();
        return ResultUtils.ok().put("data",pmsCategoryVos);
    }

    /**
     * 批量移除菜单
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public ResultUtils removeMenuByIds(@RequestBody Long[] ids){
        pmsCategoryService.removeMenuByIds(ids);
        return ResultUtils.ok("移除菜单成功");
    }

    /**
     * 新增菜单
     * @param pmsCategory
     * @return
     */
    @PostMapping("/save")
    public ResultUtils saveMenu(@RequestBody PmsCategory pmsCategory){
        pmsCategoryService.saveMenu(pmsCategory);
        return ResultUtils.ok("新增菜单成功");
    }

    /**
     * 修改菜单
     * @param pmsCategory
     * @return
     */
    @PostMapping("/update")
    public ResultUtils updateMenu(@RequestBody PmsCategory pmsCategory){
        pmsCategoryService.updateMenu(pmsCategory);
        return ResultUtils.ok("修改菜单成功");
    }

    /**
     * 批量修改菜单
     * @param pmsCategory
     * @return
     */
    @PostMapping("/update/sort")
    public ResultUtils updateMenuSort(@RequestBody PmsCategory[] pmsCategory){
        List<PmsCategory> pmsCategories = CollUtil.toList(pmsCategory);
        pmsCategoryService.updateMenuSort(pmsCategories);
        return ResultUtils.ok();
    }

    @RequestMapping("/info/{catId}")
    public ResultUtils getMenuOne(@PathVariable("catId") Integer catId){
        PmsCategory pmsCategory = pmsCategoryService.getMenuOne(catId);
        return ResultUtils.ok().put("data",pmsCategory);
    }
}
